package com.offcn.dao.daoimpl;

import com.offcn.dao.BaseDao;
import com.offcn.dao.UserDao;
import com.offcn.pojo.CourseUser;
import com.offcn.pojo.User;
import com.offcn.util.PageUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
    /*登录注册功能*/
    @Override
    public int register(String name, String phone, String pwd) {
        String sql = "INSERT INTO USER (NAME,phone,PASSWORD,status,createtime,role) VALUE (?,?,?,?,?,?) ";
        int insert = update(sql, name, phone, pwd,1,new Date(),3);
        return insert;
    }
    /*手机号登录功能*/
    @Override
    public User loginByPhone(String phone, String pwd) {
        String sql = "SELECT * FROM user WHERE phone = ? AND password = ?";
        User getUser = getBean(sql, phone, pwd);
        return getUser;
    }

    /*账号密码登录功能*/
    @Override
    public User login(String username, String password) {
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
        User getUser = getBean(sql, username, password);
        return getUser;
    }

    @Override
    public int addUser(User user) {
        String sql = "INSERT INTO `user` (\n" +
                "  `name`,\n" +
                "  `phone`,\n" +
                "  `age`,\n" +
                "  `sex`,\n" +
                "  `username`,\n" +
                "  `password`,\n" +
                "  `status`,\n" +
                "  `role`,\n" +
                "  `createtime`\n" +
                ") \n" +
                "VALUES\n" +
                "  (?,?,?,?,?,?,?,?,?)";
        int add = update(sql, user.getName(), user.getPhone(), user.getAge(),
                user.getSex(), user.getUsername(), user.getPassword()
                , user.getStatus(), user.getRole(), new Date());
        return add;
    }

    /*查询用户总条数*/
    @Override
    public Integer queryCount(String search) {
        //定义一个可变字符串
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT COUNT(*) FROM USER ");
        /*根据条件判断*/
        if (search != null && search.trim().length() != 0) {
            stringBuilder.append(" WHERE NAME LIKE CONCAT('%',?,'%')");
        }
        String sql = stringBuilder.toString();
        int i = getNumber(sql);
        return i;
    }

    /*分页查询*/
    @Override
    public List<User> queryUserByPage(PageUtil<User> pageUtil, String search) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT * FROM USER ");
        /*根据条件判断*/
        if (search != null && search.trim().length() != 0) {
            stringBuilder.append(" WHERE NAME LIKE CONCAT('%'," + search + ",'%')");
        }
        stringBuilder.append(" LIMIT ?,?");
        String sql = stringBuilder.toString();
        List<User> userList = getBeanList(sql, pageUtil.getStartIndex(), pageUtil.getPageSize());
        return userList;
    }

    @Override
    public int deleteUserByIds(String ids) {
        String sql = "delete from user where uid in (" + ids + ")";
        int delete = update(sql);
        return delete;
    }

    @Override
    public int updateUser(User user) {
        String sql = "UPDATE \n" +
                " `user` \n" +
                "SET\n" +
                "  `name` = ?,\n" +
                "  `phone` = ?,\n" +
                "  `age` = ?,\n" +
                "  `sex` = ?,\n" +
                "  `username` = ?,\n" +
                "  `password` = ?,\n" +
                "  `status` = ?,\n" +
                "  `createtime` = ?,\n" +
                "  `role` = ?,\n" +
                "  `picture` = ? \n" +
                "WHERE `uid` = ? ";
        int update = update(sql, user.getName(), user.getPhone(), user.getAge(), user.getSex(),
                user.getUsername(), user.getPassword(), user.getStatus(), new Date(), user.getRole(), user.getPicture(), user.getUid());
        return update;
    }

    @Override
    public List<Integer> queryAll(String search) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT * FROM USER ");
        if (search != null && search.trim().length() != 0) {
            stringBuilder.append("WHERE NAME LIKE CONCAT('%','" + search + "','%')");
        }

        String sql = stringBuilder.toString();
        List<User> userList = getBeanList(sql);
        ArrayList<Integer> list = new ArrayList<>();
        for (User user : userList) {
            list.add(user.getUid());
        }
        return list;
    }

    @Override
    public User queryUserById(Integer uid) {
        String Sql = "SELECT * FROM USER WHERE uid= ?";
        User user = getBean(Sql, uid);
        return user;
    }

    //手机号验证

    @Override
    public User checkPhone(String phone) {
        String sql = "SELECT * FROM USER WHERE phone=?";
        User user = getBean(sql, phone);
        return user;
    }
}
